package org.stragma.symbian.aterms;

import aterm.*;

public abstract class ATermStrategoStat implements IATermStrategoStat {

	@Override
	public long getNumberOfSpecificTerms(ATerm term) {
		long noOfStrategies = 0;
						
		switch (term.getType()) {
							
			case ATerm.APPL:
				
				ATermAppl appl = (ATermAppl)term;
				
				if (this.isTermName(appl.getAFun())) {
					noOfStrategies = 1;
				}
				
				int arity = appl.getArity();
				for(int i = 0; i < arity; i++) {
					noOfStrategies += this.getNumberOfSpecificTerms(appl.getArgument(i));
				}
				
				return noOfStrategies;
				
			case ATerm.LIST:
				
				ATermList list = (ATermList)term;
				while(!list.isEmpty()) {
					noOfStrategies += this.getNumberOfSpecificTerms(list.getFirst());	
					list = list.getNext();
				}
				
				return noOfStrategies;
				
			default:
				return 0;
		}
	
	}
	
	/**
	 * A child class overrides this method to provide the term name check compatibility
	 * @param termname
	 * @return
	 */
	protected abstract boolean isTermName(AFun fun);
}
